package com.iman.Confucius.web.student;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.iman.Confucius.domain.Note;
import com.iman.Confucius.service.student.NoteService;
import com.iman.Confucius.vo.student.CourseSelectVo;
import com.iman.Confucius.vo.student.NoteVo;
import com.iman.Confucius.web.BaseController;
import com.iman.common.support.Page;

/**
 * *************************************************************************
 * 
 * @文件名称: StudyNotesController.java
 *
 * @包路径 : com.iman.Confucius.web.student
 *
 * @版权所有: TSW 科技有限公司 (C) 2014
 *
 * @类描述: 学习笔记
 * 
 * @创建人: wangxiaolei
 *
 * @创建时间: 2014年12月18日 - 上午1:52:58
 *
 * @修改记录: 
 *        ------------------------------------------------------------------------
 *        ----------------------- 时间 | 修改人 | 修改的方法 | 修改描述
 *        ------------------------
 *        ------------------------------------------------
 *        ----------------------- | | |
 *        ------------------------------------------
 *        -----------------------------------------------------
 * 
 **************************************************************************
 */

@Controller
@RequestMapping(value = "/student/notes/*")
public class StudyNotesController extends BaseController {

	@Autowired
	private NoteService noteService;

	/**
	 * 
	 * Function: 功能说明： 使用说明：
	 * 
	 * @author wangxiaolei DateTime 2014年12月16日 下午2:59:48 返回类型: ModelAndView
	 * @param request
	 * @param menu
	 * @return
	 */
	@RequestMapping(value = "/init")
	public ModelAndView initMyNote(HttpServletRequest request, String menu,
			String type, String pn, String ps,String course_id) {
		if (type == null || "".equals(type)) {
			type = "1";
		}
		int ipn = NumberUtils.toInt(pn, 0);
		int ips = NumberUtils.toInt(ps, 10);
		Long userid = super.getUserIdInSession(request);
		Map model = new HashMap();
		model.put("user", "测试传递变量");
		ModelAndView view = null;
		view = new ModelAndView("student/my_note", model);
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("pn", ipn);
		params.put("ps", ips);
		if(!StringUtils.isBlank(course_id) && !"-1".equals(course_id)){
			params.put("course_id", Long.parseLong(course_id));
		}
		Page<NoteVo> vos = new Page<NoteVo>();
		if ("2".equals(type)) {
			vos = noteService.getAllNoteList(params);
		} else {
			params.put("user_id", userid);
			vos = noteService.getMyNoteList(params);
		}
		putResourceSrc(view);
		view.addObject("pageData", vos);
		List<CourseSelectVo> list = noteService.getCourseList(userid);
		view.addObject("course", list);
		view.addObject("type", type);
		view.addObject("course_id", course_id);
		return view;
	}

	@RequestMapping(value = "/course")
	@ResponseBody
	public Map<String, Object> getCourseList(HttpServletRequest request) {
		Long userid = super.getUserIdInSession(request);
		List<CourseSelectVo> list = noteService.getCourseList(userid);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("data", list);
		return map;
	}

	@RequestMapping(value = "/delnote")
	@ResponseBody
	public Map<String, Object> delnote(HttpServletRequest request, String id) {
		noteService.delNote(id);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("result", "success");
		return map;
	}

	@RequestMapping(value = "/coolnote")
	@ResponseBody
	public Map<String, Object> coolnote(HttpServletRequest request, String id) {
		noteService.coolNote(id);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("result", "success");
		return map;
	}
	@RequestMapping(value = "/sharenote")
	@ResponseBody
	public Map<String, Object> sharenote(HttpServletRequest request, String id,String type) {
		noteService.shareNote(id,type);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("result", "success");
		return map;
	}
	@RequestMapping(value = "/addnote")
	@ResponseBody
	public Map<String, Object> addNote(HttpServletRequest request,
			String course_id, String name, String content, String share) {
		Long userid = super.getUserIdInSession(request);
		Note note = new Note();
		note.setCourse_id(Long.parseLong(course_id));
		note.setNote_nm(name);
		note.setNote_txt(content);
		note.setShare_yn(NumberUtils.toInt(share, 0));
		note.setUser_id(userid);
		noteService.insertNote(note);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("result", "success");
		return map;
	}
	@RequestMapping(value = "/shortlist/{courseId}/{pn}/{ps}")
	public ModelAndView shortlist(HttpServletRequest request,
			@Valid    @PathVariable  	String	courseId,
			@Valid    @PathVariable  	String	pn,
			@Valid    @PathVariable  	String	ps) {
		int ipn = NumberUtils.toInt(pn, 0);
		int ips = NumberUtils.toInt(ps, 10);
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("pn", ipn);
		params.put("ps", ips);
		params.put("course_id", Long.parseLong(courseId));
		Page<NoteVo> vos =noteService.getAllNoteList(params);
		Map model = new HashMap();
		model.put("user", "测试传递变量");
		ModelAndView view = null;
		view = new ModelAndView("student/my_course_videolistnote", model);
		putResourceSrc(view);
		view.addObject("pageData", vos);
		return view;
	}
}
